package com.servlet;

import com.entity.Dto;
import com.service.CakeNameService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/CakeNameServlet")
public class CakeNameServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int displayCount=3,page=1,maxPage=0;
        CakeNameService CakeNameService = new CakeNameService();
       maxPage = CakeNameService.displayPage(displayCount);
       System.out.println("maxpage为"+maxPage);
        if (request.getParameter("page") !=null){
            int p = Integer.parseInt(request.getParameter("page"));
            if (p<1){
                p=1;
            }
            if(p>maxPage){
                p=maxPage;
            }
            page=p;
        }
        List<Dto> GoodsList = CakeNameService.LoadAllGoods(page,displayCount);
        System.out.println("GoodsList为"+GoodsList.size());
        request.getSession().setAttribute("page",page);
        request.getSession().setAttribute("maxPage",maxPage);
        request.getSession().setAttribute("GoodsList",GoodsList);
        System.out.println(GoodsList.size()+"pppppppppppppppppppppppppppp");
        for(Dto dto:GoodsList){
            System.out.println(dto.toString());
        }
        response.sendRedirect("cakeName.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
